using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Skill : MonoBehaviour
{
    [Tooltip("技能冷却时间")]
    public float cooldown;// 技能冷却时间
    public float cooldownTimer;// 冷却计时器

    protected Player player;

    protected virtual void Start()
    {
        player = PlayerManager.instance.player;
        CheckUnlock();
    }

    protected virtual void Update()
    {
        cooldownTimer -= Time.deltaTime;
    }

    // 读取数据后根据技能槽分别解锁技能
    protected virtual void CheckUnlock()
    {

    }

    public virtual bool CanUseSkill()
    {
        if (cooldownTimer < 0)
        {
            UseSkill();// 使用技能
            cooldownTimer = cooldown;// 进入冷却
            return true;
        }
        player.fx.CreatePopUpText("技能冷却中");
        return false;
    }

    //使用技能
    public virtual void UseSkill()
    {

    }

    // 寻找距离创造物最近的敌人
    protected virtual Transform FindClosestEnemy(Transform _checkTransform)
    {
        Collider2D[] colliders = Physics2D.OverlapCircleAll(_checkTransform.position, 25);// 范围内搜索敌人
        float closestDistance = Mathf.Infinity;// 正无穷
        Transform closestEnemy = null;
        foreach (var hit in colliders)// 循环范围内碰撞体
        {
            // 判断敌人范围内敌人
            if (hit.GetComponent<Enemy>() != null)
            {
                float distanceToEnemy = Vector2.Distance(_checkTransform.position, hit.transform.position);// 获取敌人距离
                if (distanceToEnemy < closestDistance)// 筛选出最短距离的敌人
                {
                    closestDistance = distanceToEnemy;
                    closestEnemy = hit.transform;
                }
            }
        }
        return closestEnemy;
    }



}
